//
// Created by 谢哲 on 2021/5/20.
//

#include <iostream>
#include <vector>
using namespace std;

vector<vector<vector<bool>>> dp_data;
vector<vector<int>> dis;

int main() {
    int n, m;
    cin >> n >> m;

    dp_data.resize(n, vector<vector<bool>>(n, vector<bool>(64, false)));
    dis.resize(n, vector<int>(n, 100));

    for (int i=0;i<m;++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        dp_data[u][v][0] = true;
        dis[u][v] = 1;
    }

    for (int k=1;k<64;++k) {
        for (int t=0;t<n;++t) {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (dp_data[i][t][k-1] && dp_data[t][j][k-1]) {
                        dp_data[i][j][k] = true;
                        dis[i][j] = 1;
                    }
                }
            }
        }
    }

    for (int k=0;k<n;++k) {
        for (int i=0;i<n;++i) {
            for (int j=0;j<n;++j) {
                dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);
            }
        }
    }

    cout << dis[0][n-1];

    return 0;
}
